﻿var CloudTalk = function(_sBaseAppURL) {
    this.m_sCloudTalkPath = _sBaseAppURL;

    /*
    * Joins a room specified by the provided name, 
    * using the provided User name and Email address
    */
    this.joinRoom = function(_sRoomName,
                             _sUserName,
                             _sUserEmail,
                             _fnSuccCallback,
                             _fnErrorCallback) {
        $.ajax({
            url: this.m_sCloudTalkPath + 'JoinRoom',
            type: "POST",
            data: {
                Name: _sUserName,
                Email: _sUserEmail,
                RoomName: _sRoomName
            },
            success: _fnSuccCallback,
            error: _fnErrorCallback,
            dataType: 'json'
        });
    };

    /*
    * Leaves a room specified by the provided name, 
    * using the provided User Email address
    */
    this.leaveRoom = function(_sRoomName,
                              _sUserEmail) {
        $.get(this.m_sCloudTalkPath + 'LeaveRoom?email=' + _sUserEmail + '&room=' + _sRoomName);
    };

    /*
    * Returns a list of available events for the provided User Email
    */
    this.getEvents = function(_sUserEmail,
                              _fnSuccCallback,
                              _fnErrorCallback) {
        $.ajax({
            url: this.m_sCloudTalkPath + 'Events?email=' + _sUserEmail + '&ts=' + new Date().getTime(),   // avoid Http GET caching
            success: _fnSuccCallback,
            error: _fnErrorCallback,
            type: "GET",
            dataType: 'json'
        });
    };

    /*
    * Sends a message in the speficified room
    */
    this.sendMessage = function(_sRoomName,
                                _sUserEmail,
                                _sMessage,
                                _fnSuccCallback,
                                _fnErrorCallback) {
        $.ajax({
            url: this.m_sCloudTalkPath + 'SendMessage',
            type: "POST",
            data: {
                Message: _sMessage,
                ClientEmail: _sUserEmail,
                RoomName: _sRoomName
            },
            success: _fnSuccCallback,
            error: _fnErrorCallback
        });
    };
};